package com.cans.web.pms;

import com.cans.dto.PageRes;
import com.cans.dto.ResDto;
import com.cans.model.pms.SysUser;
import com.cans.service.pms.api.ISysUserService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shencan
 * Date: 2017/11/12
 * Description:
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Resource
    private ISysUserService userService;

    /**
     * 根据ID获取人员信息
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    public ResDto get(@PathVariable String id){

        return ResDto.success(userService.findOne(id));
    }

    /**
     * 获取当前登录人员信息
     * @return
     */
    @GetMapping("/onlineUser")
    @ResponseBody
    public ResDto onlineUser(){

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        SysUser user = (SysUser) auth.getPrincipal();

        return ResDto.success(user);
    }

    /**
     * 新增人员
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResDto add(SysUser user){

        userService.saveOrUpdate(user);

        return ResDto.success("保存成功!");
    }

    /**
     * 修改人员
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResDto update(SysUser user){

        SysUser sysUser = userService.findOne(user.getId());

        sysUser.setUserName(user.getUserName());
        sysUser.setAge(user.getAge());
        sysUser.setEmail(user.getEmail());
        sysUser.setEnable(user.getEnable());
        sysUser.setPhone(user.getPhone());

        userService.saveOrUpdate(sysUser);

        return ResDto.success("保存成功!");
    }

    /**
     * 修改个人信息
     * @return
     */
    @PostMapping("/saveUserInfo")
    @ResponseBody
    public ResDto saveUserInfo(SysUser user){

        SysUser sysUser = userService.findOne(user.getId());

        sysUser.setEmail(user.getEmail());
        sysUser.setPhone(user.getPhone());
        sysUser.setPhotoId(user.getPhotoId());

        userService.saveOrUpdate(sysUser);

        return ResDto.success("保存成功!");
    }

    /**
     * 根据ID删除人员
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResDto delete(@PathVariable String id){

        userService.delete(id);

        return ResDto.success("删除成功!");
    }

    /**
     * 人员分页信息
     * @return
     */
    @GetMapping("/pageList")
    @ResponseBody
    public PageRes pageList(Integer page,Integer limit){

        Pageable pageable = new PageRequest(page - 1,limit);

        return  userService.findPage(pageable);
    }

    /**
     * 人员分页信息
     * @return
     */
    @PostMapping("/updateAttr")
    @ResponseBody
    public ResDto updateAttr(String id,String name,String value){

        SysUser one = userService.findOne(id);

        switch (name){
            case "enable":
                one.setEnable(Boolean.valueOf(value));
                break;
            case "sex":
                one.setSex(Integer.valueOf(value));
                break;
                default:
                return ResDto.fait("未查询到修改字段");
        }

        return  ResDto.success();
    }


    /**
     * 跳转到分页列表
     * @return
     */
    @GetMapping("/toPageList")
    public String toPageList(){

        return "user/userList";
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/toAdd")
    public String toAdd(){

        return "user/userAdd";
    }

    /**
     * 跳转到修改页面
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable String id,Model model){

        SysUser user = userService.findOne(id);

        model.addAttribute("user",user);

        return "user/userUpdate";
    }

    /**
     * 跳转到查看页面
     * @return
     */
    @GetMapping("/toView/{id}")
    public String toView(@PathVariable String id,Model model){

        SysUser user = userService.findOne(id);

        model.addAttribute("user",user);

        return "user/userView";
    }
}
